#include "King.hpp"
#include "CriticalException.hpp"
#include "Cell.hpp"
#include "Chessboard.hpp"

#define C_CONTAINS_PIECE(POS, COLOR)																\
	((Chess::getInstance()->getChessboard()->getCell(POS)->hasPiece()) 								\
	 && (Chess::getInstance()->getChessboard()->getCell(POS)->getPiece()->getColor() == COLOR))


King::King(Chess::Color color, const Position& pos)
	: Piece(color, Piece::KING, pos)
{}

King::~King()
{}

list<Position*>	King::getPossibleCases() const
{
	if (this->isOut())
		throw new CriticalException("KING :: getPossibleCases : Trying to get\
			possible cases while piece is out !");
	
	Position current = this->getCell()->getPosition();
	
	current.setColor(this->color);
	
	list<Position*> valables;
	
	int x, y, i, j, xtmp, ytmp;
	
	x = current.getX();
	y = current.getY();
	
	for (i = -1; i < 2 ; i++)
	{
		for (j = -1; j < 2 ; j++)
		{
			xtmp = x + i;
			ytmp = y + j;
			
			if (Position::isGood(xtmp, ytmp) && (!(xtmp == x && ytmp == y)) && 
					!C_CONTAINS_PIECE(Position(xtmp, ytmp, color), this->color))
				valables.push_back(new Position(xtmp, ytmp, this->color));
		}
	}
	
	return valables;
}
		
